package com.droid.customcalendar.cal.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateTimeUtils {

	public static Date removeTime(Date date, Locale locale) {
		Calendar c = Calendar.getInstance(locale);
		return removeTime(date, c);
	}

	private static Date removeTime(Date date, Calendar c) {
		c.setTime(date);
		c.set(Calendar.HOUR, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	public static Date getDayFromMonth(Locale locale, int year, int month, int day) {
		Calendar cal = Calendar.getInstance(locale);
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, day);
		return removeTime(cal.getTime(), locale);
	}

	public static boolean equals(Date d1, Date d2) {
		if (d1 != null && d2 != null) {
			Calendar calendar = Calendar.getInstance();
			d1 = removeTime(d1, calendar);
			d2 = removeTime(d1, calendar);
			return d1.equals(d2);
		}
		return false;
	}
	
	/** Check if date is in the same month and year with cmpDate
	 * */
	public static boolean isSameMonth(Date date, Date cmpDate) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(date);
		c2.setTime(cmpDate);
		boolean sameMonth = c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH);
		boolean sameYear = c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR);
		return sameMonth && sameYear;
	}
}
